package com.nio;

import io.netty.util.NettyRuntime;
import io.netty.util.internal.SystemPropertyUtil;

import java.nio.ByteBuffer;

/**
 * Created by Administrator on 2018/11/13/013.
 */
public class NioTest {
    public static void test01() {
        ByteBuffer bf = ByteBuffer.allocate(10);
        bf.putInt(1);
        bf.putFloat(12.2f);
        bf.putChar('你');

        bf.flip();
        //和put的类型不一致会抛异常
        System.out.println(bf.getInt());
        System.out.println(bf.getFloat());
        System.out.println(bf.getChar());
    }

    public static void test02() {

        ByteBuffer bf = ByteBuffer.allocate(10);
        for (int i = 0; i < 10; i++) {
            bf.put((byte) i);
        }
        bf.position(2);
        bf.limit(6);
        ByteBuffer slice = bf.slice();
        //更改slice的内容,对原有ByteBufer也有影响。
        for (int i = 0; i < slice.capacity(); i++) {
            //由于get(index)操作不涉及到postion的变化所以不用flip()
            byte b = slice.get(i);
            b *= 2;
            //同上
            slice.put(i, b);
        }
        //由于上面position设置的是2，如果flip的话会是position=0;limit=2
        bf.position(0);
        bf.limit(bf.capacity());
        while (bf.hasRemaining()) {
            System.out.println(bf.get());
        }
    }

    public static void main(String[] args) {
//        test02();
        int anInt = SystemPropertyUtil.getInt("io.netty.eventLoopThreads", NettyRuntime.availableProcessors());
        System.out.println(anInt);
    }
}
